from django.db import models

from Scenes.models import TestScene
from Testproject.models import TestProjectModel, TestEnvModel


# Create your models here.


class TestTask(models.Model):
    """
    测试任务表(TestTask)

    | 字段  | 类型  | 空   | 默认  | 注释  |
    | --- | --- | --- | --- | --- |
    | id  | int | 否   |     | 自增长主键ID |
    | project | ForeignKey | 否   |     | 所属项目 |
    | name | String | 否   |     | 任务名称 |
    | scene | ManyToManyField | 是   |     | 包含的测试业务 |

    """
    project = models.ForeignKey(TestProjectModel, on_delete=models.PROTECT, verbose_name="测试项目",
                                help_text="测试项目帮助")
    name = models.CharField(max_length=50, verbose_name="测试任务名称")
    scene = models.ManyToManyField(TestScene, blank=True, verbose_name="测试场景")

    def __str__(self):
        return self.name

    class Meta:
        db_table = "test_task"
        verbose_name_plural = "测试任务表"


class TestRecord(models.Model):
    """
    | 字段          | 类型         | 空   | 默认  | 注释      |
    |:----------- |:---------- |:--- | --- | ------- |
    | id          | int        | 否   |     | 自增长主键ID |
    | task        | ForeignKey | 否   |     | 执行的测试任务 |
    | all         | int        | 是   | 0   | 用例总数    |
    | success     | int        | 是   | 0   | 成功用例数   |
    | fail        | int        | 是   | 0   | 失败用例数   |
    | error       | int        | 是   | 0   | 错误用例数   |
    | pass_rate   | varchar    | 是   | '0' | 通过率     |
    | tester      | varchar    | 是   |     | 执行者     |
    | env         | ForeignKey | 否   |     | 测试环境    |
    | statue      | varchar    | 否   |     | 执行状态    |
    | create_time | DataTime   | 否   |     | 执行时间    |

    """
    task = models.ForeignKey(TestTask, on_delete=models.PROTECT, verbose_name="执行的测试任务",
                             help_text="执行的测试任务")
    all = models.IntegerField(verbose_name="用例总数", help_text="用例总数", blank=True, default=0)
    success = models.IntegerField(verbose_name="成功用例数", help_text="成功用例数", blank=True, default=0)
    fail = models.IntegerField(verbose_name="成功用例数", help_text="成功用例数", blank=True, default=0)
    error = models.IntegerField(verbose_name="错误用例数", help_text="错误用例数", blank=True, default=0)
    pass_rate = models.CharField(max_length=20, verbose_name='通过率', help_text='通过率', blank=True, default=0)
    tester = models.CharField(max_length=20, verbose_name='执行者', help_text='执行者')
    env = models.ForeignKey(TestEnvModel, on_delete=models.PROTECT, verbose_name='测试环境', help_text='测试环境')

    statue = models.CharField(max_length=20, verbose_name='执行状态', help_text='执行状态')
    create_time = models.DateTimeField(auto_created=True, verbose_name="执行时间", help_text="执行时间",
                                       auto_now_add=True)

    class Meta:
        db_table = "test_record"
        verbose_name_plural = "测试记录表"


class TestReport(models.Model):
    """
    | 字段     | 类型            | 空   | 默认  | 注释      |
    |:------ |:------------- |:--- | --- | ------- |
    | id     | int           | 否   |     | 自增长主键ID |
    | info   | JSONField     | 是   |     | 报告数据    |
    | record | OneToOneField | 是   |     | 测试记录    |
    """
    info = models.JSONField(blank=True, verbose_name="报告数据", help_text="报告数据", default=dict)
    record = models.OneToOneField(TestRecord, on_delete=models.CASCADE, blank=True, verbose_name="测试记录",
                                  help_text="测试记录")

    class Meta:
        db_table = "test_report"
        verbose_name_plural = "测试报告表"
